package com.bjb.vr.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.ao.OptHelpDocAo;
import com.bjb.vr.common.dto.OptHelpDoc;
import com.bjb.vr.common.mapper.OptHelpDocMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
* @author admin
* @description 针对表【opt_help_doc(帮助文档表)】的数据库操作Service
* @createDate 2022-06-16 15:52:08
*/

@Service
public class OptHelpDocService extends ServiceImpl<OptHelpDocMapper, OptHelpDoc> {


    public IPage<OptHelpDoc> queryHelpDocList(OptHelpDocAo optHelpDocAo){
        Page<OptHelpDoc> optHelpDocPage = new Page<>(optHelpDocAo.getCurrent(),optHelpDocAo.getSize());
        LambdaQueryWrapper<OptHelpDoc> queryWrapper = Wrappers.lambdaQuery(OptHelpDoc.class);
        //查询条件
        queryWrapper.like(StringUtils.isNotBlank(optHelpDocAo.getDocTitle()),OptHelpDoc::getDocTitle,optHelpDocAo.getDocTitle());
        //执行查询
        Page<OptHelpDoc>  optHelpDocPageResult = this.page(optHelpDocPage,queryWrapper);
        IPage<OptHelpDoc> iPage = new Page<>(optHelpDocPageResult.getCurrent(),optHelpDocPageResult.getSize(),optHelpDocPageResult.getTotal());
        iPage.setRecords(optHelpDocPageResult.getRecords());
        return iPage;
    }


    public boolean addHelpDoc(OptHelpDocAo optHelpDocAo){
        Assert.notNull(optHelpDocAo.getDocTitle(),"文档标题不能为空");
        Assert.notNull(optHelpDocAo.getDocCatalog(),"产品分类不能为空");

        OptHelpDoc optHelpDoc = new OptHelpDoc();
        optHelpDoc.setDocTitle(optHelpDocAo.getDocTitle());
        optHelpDoc.setDocCatalog(optHelpDoc.getDocCatalog());

        boolean result = this.save(optHelpDoc);
        return result;
    }


    public boolean updateHelpDoc(OptHelpDocAo optHelpDocAo){
        Assert.notNull(optHelpDocAo.getId(),"文档id不能为空");
        Assert.notNull(optHelpDocAo.getDocTitle(),"文档标题不能为空");
        Assert.notNull(optHelpDocAo.getDocCatalog(),"产品分类不能为空");
        OptHelpDoc optHelpDoc = new OptHelpDoc();
        optHelpDoc.setId(optHelpDocAo.getId());
        optHelpDoc.setDocTitle(optHelpDocAo.getDocTitle());
        optHelpDoc.setDocCatalog(optHelpDoc.getDocCatalog());

        boolean result = this.updateById(optHelpDoc);
        return result;
    }

    public boolean deleteHelpDoc(OptHelpDocAo optHelpDocAo){
        Assert.notNull(optHelpDocAo.getId(),"文档id不能为空");

        boolean result = this.removeById(optHelpDocAo.getId());
        return result;
    }

    public OptHelpDoc queryHelpDocDetail(OptHelpDocAo optHelpDocAo){
        Assert.notNull(optHelpDocAo.getId(),"文档id不能为空");
        OptHelpDoc optHelpDoc = this.getById(optHelpDocAo.getId());
        return optHelpDoc;
    }

}
